package servlets;

import java.io.IOException;
//import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Enumeration;

//import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import com.mysql.jdbc.interceptors.SessionAssociationInterceptor;

import db.Campaign;
import db.Coupons;
import db.DBHelper;
import db.FormatAndValidation;
import db.StoreLocation;
import db.UserCheckInData;
import db.UserData;
import db.Login;
import db.Registration;

/**
 * Servlet implementation class AdminServlet
 */
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	DBHelper helper = new DBHelper();
	Coupons coupon = new Coupons();
	Campaign campaign = new Campaign();
	String rootPath = "/FronteraReward";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// try {

	}

	protected void doCheckOut(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ServletContext ctx = this.getServletContext();
		// RequestDispatcher dispatcher =
		// ctx.getRequestDispatcher("/_main/server.jsp");

		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " val = "
					+ request.getParameter(parameterName));

		}
		int checkOutSuccessful = 0;
		checkOutSuccessful = helper.checkUserOut(
				Integer.parseInt(request.getParameter("checkInID")),
				request.getParameter("subTotal"),
				request.getParameter("ticketId"),
				Integer.parseInt(request.getParameter("customerID")),
				Integer.parseInt(request.getParameter("serverID")));

		if (checkOutSuccessful == 0) {
			request.setAttribute("storeID", request.getParameter("storeID"));
			request.setAttribute("serverID", request.getParameter("serverID"));
			System.out.println("check out  ");

			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/server.jsp");
			return;
		} else {
			System.out.println("not successful check out");

		}

	}

	protected void doRegistration(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Registration registration = new Registration();
		
		// ServletContext ctx = this.getServletContext();
		Enumeration<String> enumeration = request.getParameterNames();
		String errorMsg = "";
		Boolean iFound = false;
		HttpSession session = request.getSession(true);
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();
			if (request.getParameter(parameterName).equals("")) {
				errorMsg += parameterName + " must be filled in! <br>";
				iFound = true;
			}

			System.out.println("Parameter = " + parameterName);

		}

		if (iFound) {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/login.jsp");
			session.setAttribute("errorMessage", errorMsg);
			// response.
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/login.jsp");
			return;
		} else {

			int regSuccessful = 0;
			String pw = FormatAndValidation.md5(request.getParameter("password"));
			int role = 1;

			if (request.getParameter("role") != null) {
				if (Integer.parseInt(request.getParameter("role")) != 0) {
					role = Integer.parseInt(request.getParameter("role"));
				}
			}
			regSuccessful = registration.registerUsers(
					request.getParameter("fullname"),
					request.getParameter("phone"),
					request.getParameter("email"), pw,
					request.getParameter("birthday"), role);
			if (regSuccessful == 0) {
				// RequestDispatcher dispatcher =
				// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
				session.setAttribute("errorMessage",
						"Registration was successful!");
				doSetUp(request, response);
				System.out.println("insert data");
				// session.setAttribute("errorMessage","");
				// Cookie[] cookies = request.getCookies();
				// boolean foundCookie = false;

				/*
				 * for(int i = 0; i < cookies.length; i++) { Cookie cookie1 =
				 * cookies[i]; if (cookie1.getName().equals("fronterUser")) {
				 * System.out.println("fronterUser = " + cookie1.getValue());
				 * foundCookie = true; } }
				 * 
				 * if (!foundCookie) { Cookie cookie1 = new
				 * Cookie("fronterUser", request.getParameter("phone"));
				 * cookie1.setMaxAge(24*60*60); response.addCookie(cookie1); }
				 */
				// dispatcher.forward(request,response);
				response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
				return;
			} else {
				// RequestDispatcher dispatcher =
				// ctx.getRequestDispatcher("/login.jsp");
				session.setAttribute("errorMessage", "User already exist!");
				// dispatcher.forward(request,response);
				response.sendRedirect(rootPath + "/login.jsp");
				System.out.println(errorMsg);
				return;
			}

		}

	}

	protected void doLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// Create Login Object
		Login login = new Login();

		// ServletContext ctx = this.getServletContext();
		String userPath = request.getServletPath();
		System.out.println(userPath);

		HttpSession session = request.getSession(true);
		Enumeration<String> enumeration = request.getParameterNames();

		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();
			System.out.println("Parameter = " + parameterName);
		}
		System.out.println(request.getParameter("adminRequestType"));
		System.out.println(request.getParameter("phone") + ": test : "
				+ request.getParameter("password"));
		if (request.getParameter("adminRequestType").equals("login")) {
			System.out.println("here login");
			String adminUserName = request.getParameter("phone");
			String adminPassword = request.getParameter("password");
			// System.out.println("Delete ID:" +
			// request.getParameter("deleteID"));
			if (adminUserName.equals("") || adminPassword.equals("")) {
				System.out.println("here blank");
				// RequestDispatcher dispatcher =
				// ctx.getRequestDispatcher("/login.jsp");
				session.setAttribute("errorMessage",
						"Incorrect login or password!");
				// response.
				// dispatcher.forward(request,response);
				response.sendRedirect(rootPath + "/login.jsp");
				return;
			} else {
				System.out.println("here none");
				int validateUserLogin = 0;
				String pw = FormatAndValidation.md5(adminPassword);
				validateUserLogin = login.validateUserLogin(adminUserName, pw);
				if (validateUserLogin == 1) {

					System.out.println("MD5 in hex: " + pw);
					/*
					 * System.out.println("here admin"); //RequestDispatcher
					 * dispatcher =
					 * ctx.getRequestDispatcher("/_main/userHomePage.jsp");
					 * //Cookie cookie1 = new
					 * Cookie("fronterUser",request.getParameter("phone") );
					 * //cookie1.setMaxAge(24*60*60);
					 * //response.addCookie(cookie1); int validate = 0; int
					 * storeID = 0; int checkinID = 0; ArrayList<UserData>
					 * userInfo = new ArrayList<UserData>(); ArrayList<Coupons>
					 * couponInfo = new ArrayList<Coupons>();
					 * ArrayList<StoreLocation> storeInfo = new
					 * ArrayList<StoreLocation>();
					 * 
					 * ArrayList<UserCheckInData> userCheckInInfo = new
					 * ArrayList<UserCheckInData>();
					 * 
					 * couponInfo=helper.getCouponsinfo();
					 * userInfo=helper.getUserInfo
					 * (request.getParameter("phone"));
					 * storeInfo=helper.getStoreInfo();
					 * 
					 * int custID = userInfo.get(0).getUserTableId(); validate =
					 * helper.validateIfUserIsCheckedIn(custID);
					 * session.setAttribute("sesCustID",custID);//set cust id
					 * int adminRights = 1; int serverRights = 1; int _serverId
					 * = 0;
					 * 
					 * session.setAttribute("_serverId", _serverId); if(validate
					 * == 1) { userCheckInInfo =
					 * helper.getUserCheckInData(custID); storeID =
					 * userCheckInInfo.get(0).getStoreLocationId(); checkinID =
					 * userCheckInInfo.get(0).getCheckInTableId();
					 * 
					 * session.setAttribute("storeCkInId", storeID);
					 * session.setAttribute("checkinID", checkinID);
					 * session.setAttribute("displayRewardsSec","inline");
					 * session.setAttribute("sesCustID",custID); }
					 * if(adminRights == 1) {
					 * session.setAttribute("adminRights", adminRights);
					 * session.setAttribute("_serverId", custID); }
					 * if(serverRights == 1) {
					 * session.setAttribute("serverRights", serverRights);
					 * session.setAttribute("_serverId", custID);
					 * 
					 * } session.setAttribute("userInfo", userInfo);
					 * session.setAttribute("couponInfo", couponInfo);
					 * session.setAttribute("storeInfo", storeInfo);
					 * 
					 * session.setAttribute("adminRights", adminRights);
					 * session.setAttribute("serverRights", serverRights);
					 * request
					 * .setAttribute("userName",request.getParameter("phone"));
					 */
					doSetUp(request, response);
					session.setAttribute("errorMessage", "");

					// String v = request.getPathInfo().substring(1);
					// request.getRequestDispatcher("/_main/userHomePage.jsp").forward(request,
					// response);
					response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
					return;

					// } else {
					// response.sendRedirect(view); // We'd like to fire
					// redirect in case of a view change as result of the action
					// (PRG pattern).
					// }
					// dispatcher.forward(request,response);
				} else {
					System.out.println("here index");
					// RequestDispatcher dispatcher =
					// ctx.getRequestDispatcher("/login.jsp");

					session.setAttribute("errorMessage",
							"Incorrect login or password!");
					// dispatcher.forward(request,response);
					response.sendRedirect(rootPath + "/login.jsp");
					return;
				}
			}
		}

	}

	/**
	 * @see HttpServlet#doCookieLogin(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doCookieLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String userPath = request.getServletPath();
		System.out.println(userPath);

		HttpSession session = request.getSession(true);
		Enumeration<String> enumeration = request.getParameterNames();

		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();
			System.out.println("Parameter = " + parameterName);
		}
		System.out.println(request.getParameter("adminRequestType"));
		System.out.println(request.getParameter("phone") + ": test : ");
		if (request.getParameter("adminRequestType").equals("cookie")) {
			System.out.println("here cookie");
			String adminUserName = request.getParameter("phone");
			if (adminUserName.equals("")) {
				System.out.println("here cookie blank");
				session.setAttribute("errorMessage",
						"Session expired please login!");
				response.sendRedirect(rootPath + "/login.jsp");
				return;
			} else {
				System.out.println("get user data");

				doSetUp(request, response);
				session.setAttribute("errorMessage", "");

				response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
				return;

			}
		}

	}

	/**
	 * @see HttpServlet#doSetUp(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doSetUp(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		System.out.println("here admin");
		// RequestDispatcher dispatcher =
		// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
		// Cookie cookie1 = new
		// Cookie("fronterUser",request.getParameter("phone") );
		// cookie1.setMaxAge(24*60*60);
		// response.addCookie(cookie1);
		int validate = 0;
		int storeID = 0;
		int checkinID = 0;
		ArrayList<UserData> userInfo = new ArrayList<UserData>();
		ArrayList<Coupons> couponInfo = new ArrayList<Coupons>();
		ArrayList<StoreLocation> storeInfo = new ArrayList<StoreLocation>();
		ArrayList<Campaign> campaignInfo = new ArrayList<Campaign>();
		ArrayList<UserData> customerInfo = new ArrayList<UserData>();

		ArrayList<UserCheckInData> userCheckInInfo = new ArrayList<UserCheckInData>();
		String phoneNum = "0";
		if (request.getParameter("phone") != null) {
			phoneNum = request.getParameter("phone");
		}
		if (session.getAttribute("userName") != null) {
			Object g = session.getAttribute("userName");
			phoneNum = g.toString();

		}
		couponInfo = coupon.getCouponsinfo();
		userInfo = helper.getUserInfo(phoneNum);
		storeInfo = helper.getStoreInfo();
		campaignInfo = campaign.getAllCampaigninfo();
		customerInfo = helper.getAllCustomerInfo();

		int custID = userInfo.get(0).getUserTableId();
		validate = helper.validateIfUserIsCheckedIn(custID);
		session.setAttribute("sesCustID", custID);// set cust id

		int adminRights = 0;
		int serverRights = 0;
		int _serverId = 0;
		if (userInfo.get(0).getUserRole() == 2) {
			serverRights = 1;
		}
		if (userInfo.get(0).getUserRole() == 3) {
			adminRights = 1;
			serverRights = 1;
		}

		session.setAttribute("_serverId", _serverId);
		if (validate == 1) {
			userCheckInInfo = helper.getUserCheckInData(custID);
			storeID = userCheckInInfo.get(0).getStoreLocationId();
			checkinID = userCheckInInfo.get(0).getCheckInTableId();
			session.setAttribute("userName", phoneNum);
			session.setAttribute("storeCkInId", storeID);
			session.setAttribute("checkinID", checkinID);
			session.setAttribute("displayRewardsSec", "inline");
			session.setAttribute("sesCustID", custID);
		}
		if (adminRights == 1) {
			session.setAttribute("adminRights", adminRights);
			session.setAttribute("_serverId", custID);
		}
		if (serverRights == 1) {
			session.setAttribute("serverRights", serverRights);
			session.setAttribute("_serverId", custID);

		}
		session.setAttribute("userInfo", userInfo);
		session.setAttribute("couponInfo", couponInfo);
		session.setAttribute("storeInfo", storeInfo);
		session.setAttribute("campaignInfo", campaignInfo);
		session.setAttribute("customerInfo", customerInfo);

		session.setAttribute("adminRights", adminRights);
		session.setAttribute("serverRights", serverRights);
		session.setAttribute("userName", phoneNum);

		// request.setAttribute("userName",request.getParameter("phone"));

	}

	/**
	 * @see HttpServlet#doDeleteCust(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doDeleteActCust(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		int custID = Integer.parseInt(request.getParameter("custID"));
		// int location = Integer.parseInt(request.getParameter("location"));
		int statusType = Integer.parseInt(request.getParameter("statusType"));

		// ServletContext ctx = this.getServletContext();

		// int custID = Integer.parseInt(request.getParameter("custID"));

		Enumeration<String> enumeration = request.getParameterNames();
		// String errorMsg = "";
		// Boolean iFound = false;
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

		}
		int deleteUserComplete = 0;

		deleteUserComplete = helper.deactivateUser(custID, statusType);
		System.out.println("do checkin");
		if (deleteUserComplete == 10) {
			response.sendRedirect(rootPath + "/errorPage.jsp");
			return;
		} else {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			// session.setAttribute("displayRewardsSec","inline");
			// session.setAttribute("storeCkInId", storeID);
			doSetUp(request, response);
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/admin-view-customer.jsp");
			System.out.println("customer deleted");
			return;
		}

	}

	/**
	 * @see HttpServlet#doDeleteCust(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doDeleteCoupon(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		int ID = Integer.parseInt(request.getParameter("typeID")); // tableid
		String typeName = request.getParameter("typeName");// table to use
		// int statusType = 1;//deactivate

		// ServletContext ctx = this.getServletContext();

		// int custID = Integer.parseInt(request.getParameter("custID"));

		Enumeration<String> enumeration = request.getParameterNames();
		// String errorMsg = "";
		// Boolean iFound = false;
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

		}
		int removeTypeComplete = 0;

		if (typeName.equals("editPromo")) {
			removeTypeComplete = coupon.removeCoupon(ID);
			int o = 9;
		} else {
			removeTypeComplete = campaign.removeCampaign(ID);
			int t = 9;
		}

		System.out.println("do checkin");
		if (removeTypeComplete == 10) {
			response.sendRedirect(rootPath + "/errorPage.jsp");
			return;
		} else {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			// session.setAttribute("displayRewardsSec","inline");
			// session.setAttribute("storeCkInId", storeID);
			doSetUp(request, response);
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/admin-view.jsp");
			System.out.println("coupon / campaign deleted");
			return;
		}

	}

	/**
	 * @see HttpServlet#doAddEditCoupon(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doAddEditCoupon(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);

		int ID = 0; // Integer.parseInt(request.getParameter("typeID"));
					// //tableid
		int isUpdate = 0;
		String typeName = "";// request.getParameter("typeName");//table to use
		// int statusType = 1;//deactivate

		if (request.getParameter("isUpdate") != null) {
			isUpdate = Integer.parseInt(request.getParameter("isUpdate"));
		}
		if (request.getParameter("typeID") != null) {
			ID = Integer.parseInt(request.getParameter("typeID"));
		}

		if (request.getParameter("typeName") != null) {
			typeName = request.getParameter("typeName");
		}

		ServletContext ctx = this.getServletContext();

		// int custID = Integer.parseInt(request.getParameter("custID"));

		Enumeration<String> enumeration = request.getParameterNames();
		// String errorMsg = "";
		Boolean iFound = false;
		String errorMsg = "";

		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();
			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

			if (parameterName.equals("promoname")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "Promo Name </br>";
				iFound = true;
				// break;
			}
			if (parameterName.equals("promoamount")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "Pesos Amount </br>";
				iFound = true;
				// break;
			}
			if (parameterName.equals("promostartdate")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "Start Date </br>";
				iFound = true;
				// break;
			} else if (parameterName.equals("promostartdate")
					&& !FormatAndValidation.validateUserDate(request
							.getParameter(parameterName))) {
				errorMsg += "Start Date " + FormatAndValidation.getErrorMessage()
						+ " </br>";
				iFound = true;
				// break;
			}

			if (parameterName.equals("promoenddate")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "End Date </br>";
				iFound = true;
				// break;
			}
			// campaign
			if (parameterName.equals("campaignname")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "Campaign Name  </br>";
				iFound = true;
				// break;
			}
			if (parameterName.equals("pesoamount")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "Pesos Multiplier  </br>";
				iFound = true;
				// break;
			}
			if (parameterName.equals("campaigndatestart")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "Start Date </br>";
				iFound = true;
				// break;
			}
			if (parameterName.equals("campaigndateend")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "End Date </br>";
				iFound = true;
				// break;
			}

			/*
			 * Parameter = campaignname // Parameter = campaigndatestart //
			 * Parameter = setbirthdaypromo // 0 Parameter = setinitialstatus //
			 * 1 Parameter = typeName // campaign Parameter = pesoamount //
			 * Parameter = adminRequestType // addCoupon Parameter = isUpdate //
			 * 0 campaigndateend
			 */
		}

		int addCouponComplete = 0;
		// Promo Name
		// Pesos Amount
		// // Start Date
		// End Date
		/*
		 * Parameter = promoname // Parameter = promoenddate // Parameter =
		 * promostartdate // Parameter = setbirthdaypromo // no Parameter =
		 * setinitialstatus // inactive
		 */
		if (iFound) {
			if (typeName.equals("promo") && isUpdate == 0) {
				session.setAttribute("errorMessage",
						"Please fix the following field(s)! </br>" + errorMsg);
				// String url = "/_main/admin-view.jsp";
				response.sendRedirect(rootPath + "/_main/admin-view.jsp");
				return;
			} else if (typeName.equals("promo") && isUpdate == 1) {
				session.setAttribute("errorMessage",
						"Please fix the following field(s)! </br>" + errorMsg);
				response.sendRedirect(rootPath
						+ "/_main/admin-viewEditPromo.jsp?aID="
						+ request.getParameter("aID") + "&itemID="
						+ request.getParameter("typeID"));
				return;
			} else if (typeName.equals("campaign") && isUpdate == 0) {
				session.setAttribute("errorMessage",
						"Please fix the following field(s)! </br>" + errorMsg);
				// String url = "/_main/admin-view.jsp";
				response.sendRedirect(rootPath + "/_main/admin-view.jsp");
				return;
			} else if (typeName.equals("campaign") && isUpdate == 1) {
				session.setAttribute("errorMessage",
						"Please fix the following field(s)! </br>" + errorMsg);
				response.sendRedirect(rootPath
						+ "/_main/admin-viewEditCampaign.jsp?aID="
						+ request.getParameter("aID") + "&itemID="
						+ request.getParameter("typeID"));
				return;
			}
			// RequestDispatcher dispatcher = ctx.getRequestDispatcher(url);
			// dispatcher.forward(request,response);
		}
		
		if (typeName.equals("promo")) {
			addCouponComplete = coupon.addEditCoupons(ID, isUpdate,
					request.getParameter("promoname"),
					Integer.parseInt(request.getParameter("promoamount")), 0,
					request.getParameter("promostartdate"),
					request.getParameter("promoenddate"),
					Integer.parseInt(request.getParameter("setinitialstatus")),
					1,
					Integer.parseInt(request.getParameter("setbirthdaypromo")));
			int o = 9;
		} else {
			addCouponComplete = campaign.addEditCampaign(ID, isUpdate,
					request.getParameter("campaignname"), "",
					Integer.parseInt(request.getParameter("pesoamount")),
					request.getParameter("campaigndatestart"),
					request.getParameter("campaigndateend"),
					Integer.parseInt(request.getParameter("setinitialstatus")),
					1,
					Integer.parseInt(request.getParameter("setbirthdaypromo")));

			int t = 9;
		}

		if (addCouponComplete == 10) {
			response.sendRedirect(rootPath + "/errorPage.jsp");
			return;
		} else {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			// session.setAttribute("displayRewardsSec","inline");
			// session.setAttribute("storeCkInId", storeID);
			doSetUp(request, response);
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/admin-view.jsp");
			System.out.println("coupon / campaign deleted");
			return;
		}

	}

	/**
	 * @see HttpServlet#doSearchCust(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doSearchCust(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);

		int ID = 0; // Integer.parseInt(request.getParameter("typeID"));
					// //tableid
		String typeName = "";// request.getParameter("typeName");//table to use
		// int statusType = 1;//deactivate

		if (request.getParameter("typeName") != null) {
			typeName = request.getParameter("typeName");
		}

		ServletContext ctx = this.getServletContext();

		// int custID = Integer.parseInt(request.getParameter("custID"));

		Enumeration<String> enumeration = request.getParameterNames();
		// String errorMsg = "";
		Boolean iFound = false;
		String errorMsg = "";
		int countEntries = 0;
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();
			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

			if (parameterName.equals("customeremail")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "Customer Email </br>";
				countEntries = countEntries + 1;
				// break;
			}
			if (parameterName.equals("phonenumber")
					&& request.getParameter(parameterName).equals("")) {
				errorMsg += "or Phone Number </br>";
				countEntries = countEntries + 1;
				// break;
			}

		}

		int searchComplete = 0;

		if (countEntries == 2) {

			session.setAttribute("errorMessage",
					"Please fill in one of the following fields! </br>"
							+ errorMsg);
			response.sendRedirect(rootPath + "/_main/admin-view.jsp");
			return;
		}
		searchComplete = helper.searchCustomerData(
				request.getParameter("phonenumber"),
				request.getParameter("customeremail"));

		if (searchComplete == -2) {
			// response.sendRedirect(rootPath+"/errorPage.jsp");
			return;
		} else if (searchComplete == -1) {
			session.setAttribute("errorMessage", "Customer not found!");
			response.sendRedirect(rootPath + "/_main/admin-view.jsp");
			return;
		} else {

			doSetUp(request, response);
			response.sendRedirect(rootPath + "/_main/editProfile.jsp?itemID="
					+ searchComplete);
			System.out.println("search customers");
			return;
		}

	}

	/**
	 * @see HttpServlet#doCheckIn(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doCheckIn(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		int storeID = Integer.parseInt(request.getParameter("location"));
		// int location = Integer.parseInt(request.getParameter("location"));

		Object g = session.getAttribute("sesCustID");
		int custID = Integer.parseInt(g.toString());

		// ServletContext ctx = this.getServletContext();

		// int custID = Integer.parseInt(request.getParameter("custID"));

		Enumeration<String> enumeration = request.getParameterNames();
		// String errorMsg = "";
		// Boolean iFound = false;
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

		}
		System.out.println("do checkin");
		int validateCheckIn = 0;
		validateCheckIn = helper.checkUserIn(custID, storeID);
		if (validateCheckIn == 10) {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			// request.setAttribute("errorMessage","You are checked in already!");
			session.setAttribute("displayRewardsSec", "inline");
			session.setAttribute("storeCkInId", storeID);

			session.setAttribute("isCheckedIn", 1);

			System.out.println("checked in already");
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
			return;
		} else {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			// session.setAttribute("displayRewardsSec","inline");
			// session.setAttribute("storeCkInId", storeID);
			doSetUp(request, response);
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
			System.out.println("checked me in");
			return;
		}

	}

	/**
	 * @see HttpServlet#doEditProfileUpdate(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doEditProfileUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * int storeID = Integer.parseInt(request.getParameter("location")); int
		 * location = Integer.parseInt(request.getParameter("location")); int
		 * custID = Integer.parseInt(request.getParameter("custID"));
		 * HttpSession session = request.getSession(true);
		 */
		// ServletContext ctx = this.getServletContext();
		Enumeration<String> enumeration = request.getParameterNames();
		// String errorMsg = "";
		// Boolean iFound = false;
		HttpSession session = request.getSession(true);
		ArrayList<UserData> userInfo = new ArrayList<UserData>();

		if (session.getAttribute("userInfo") != null) {
			Object t = session.getAttribute("userInfo");
			userInfo = (ArrayList<UserData>) t;
			for (int i = 0; i < userInfo.size(); i++) {
				System.out.println(" userdata "
						+ userInfo.get(i).getPhoneNumber());
			}
		}
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

		}
		int updateSuccessful = 0;
		// int custID = Integer.parseInt(request.getParameter("custID"));
		Object g = session.getAttribute("sesCustID");
		int custID = Integer.parseInt(g.toString());
		String pw = "";
		if (request.getParameter("password").equals(
				userInfo.get(0).getPassword())) {
			pw = userInfo.get(0).getPassword();
		} else {
			pw = FormatAndValidation.md5(request.getParameter("password"));
		}

		int role = userInfo.get(0).getUserRole();
		if (request.getParameter("role") != null) {
			if (Integer.parseInt(request.getParameter("role")) != 0) {
				role = Integer.parseInt(request.getParameter("role"));
			}
		}
		updateSuccessful = helper.updateUserData(
				request.getParameter("fullname"),
				request.getParameter("phone"), request.getParameter("email"),
				pw, request.getParameter("birthday"), custID, role);

		if (updateSuccessful == 10) {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/editProfile.jsp");
			request.setAttribute("errorMessage", "Update was unsuccessful!");
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/editProfile.jsp");
			return;

		} else {
			userInfo = helper.getUserInfo(request.getParameter("phone"));
			session.setAttribute("userInfo", userInfo);
			session.setAttribute("storeCkInId",
					request.getParameter("storeCkInId"));

			request.setAttribute("userName", request.getParameter("phone"));

			request.setAttribute("errorMessage", "Update was successful!");
			response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
			return;
		}
		/*
		 * Parameter = fullname // larry benjamin Parameter = phone //
		 * 678-523-7250 Parameter = email // lbenjamin0@gmail.com Parameter =
		 * password // admin Parameter = birthday // 3/3 Parameter =
		 * adminRequestType // editprofile
		 */
	}

	/**
	 * @see HttpServlet#doCheckOut(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doResetLocation(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * int storeID = Integer.parseInt(request.getParameter("location")); int
		 * location = Integer.parseInt(request.getParameter("location"));
		 * 
		 * HttpSession session = request.getSession(true);
		 */

		HttpSession session = request.getSession(true);

		// int custID = Integer.parseInt(request.getParameter("custID"));
		Object g = session.getAttribute("sesCustID");
		int custID = Integer.parseInt(g.toString());
		// ServletContext ctx = this.getServletContext();
		Enumeration<String> enumeration = request.getParameterNames();
		// String errorMsg = "";
		// Boolean iFound = false;

		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

		}
		int resetLocationsuccessful = 0;
		// int custID = Integer.parseInt(request.getParameter("custID"));
		resetLocationsuccessful = helper.resetCheckInLocation(custID);

		if (resetLocationsuccessful == 10) {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			request.setAttribute("errorMessage", "Reset was un-successful!");
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
			return;

		} else {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			session.setAttribute("storeCkInId", 0);
			request.setAttribute("errorMessage", "Reset was successful!");
			session.setAttribute("displayRewardsSec", "none");

			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
			return;
		}
		/*
		 * Parameter = fullname // larry benjamin Parameter = phone //
		 * 678-523-7250 Parameter = email // lbenjamin0@gmail.com Parameter =
		 * password // admin Parameter = birthday // 3/3 Parameter =
		 * adminRequestType // editprofile
		 */
	}

	/**
	 * @see HttpServlet#doRedemption(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doRedemption(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * int storeID = Integer.parseInt(request.getParameter("location")); int
		 * location = Integer.parseInt(request.getParameter("location"));
		 * 
		 * 
		 * HttpSession session = request.getSession(true);
		 */

		Enumeration<String> enumeration = request.getParameterNames();
		// Boolean iFound = false;

		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

		}
		HttpSession session = request.getSession(true);
		int couponID = 0;
		// if (request.getParameter("couponID")!=null)
		// {
		couponID = Integer.parseInt(request.getParameter("couponID"));
		// }

		Object g = session.getAttribute("checkinID");
		int checkinID = Integer.parseInt(g.toString());

		// ServletContext ctx = this.getServletContext();

		int Redemptionsuccessful = 0;
		// int custID = Integer.parseInt(request.getParameter("custID"));

		Redemptionsuccessful = helper.addSelectedReward(couponID, checkinID);

		if (Redemptionsuccessful == 10) {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			request.setAttribute("errorMessage",
					"Redemption was un-successful!");
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
			return;
		} else {
			// RequestDispatcher dispatcher =
			// ctx.getRequestDispatcher("/_main/userHomePage.jsp");
			request.setAttribute("errorMessage", "Redemption was successful!");
			// dispatcher.forward(request,response);
			response.sendRedirect(rootPath + "/_main/userHomePage.jsp");
			return;
		}
		/*
		 * Parameter = fullname // larry benjamin Parameter = phone //
		 * 678-523-7250 Parameter = email // lbenjamin0@gmail.com Parameter =
		 * password // admin Parameter = birthday // 3/3 Parameter =
		 * adminRequestType // editprofile
		 */
	}

	/**
	 * @see HttpServlet#setFolderPath(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	private void setFolderPath(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// TODO Auto-generated method stub
		System.out.println("do set folder path 1");
		HttpSession session = request.getSession(true);
		Enumeration<String> enumeration = request.getParameterNames();

		session.removeAttribute("errorMessage");
		if (session.getAttribute("fronteraFolder") != null) {

			Object g = session.getAttribute("fronteraFolder");
			rootPath = "/" + g;

		} else if (request.getParameter("fronteraFolder") != null) {
			rootPath = "/" + request.getParameter("fronteraFolder");
			session.setAttribute("fronteraFolder",
					request.getParameter("fronteraFolder"));
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("do checkin 1");
		InetAddress addr = InetAddress.getLocalHost();
		// byte[] ipAddr = addr.getAddress();
		String hostname = addr.getHostName();
		System.out.println("hostname=" + hostname);
		HttpSession session = request.getSession(true);

		ServletContext ctx = this.getServletContext();
		// rootPath = ctx.get
		// String serverName = request.getRemoteHost();

		// ServletContext ctx = this.getServletContext();
		Enumeration<String> enumeration = request.getParameterNames();
		// Boolean iFound = false;

		session.removeAttribute("errorMessage");
		setFolderPath(request, response);

		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			System.out.println("Parameter = " + parameterName + " //  "
					+ request.getParameter(parameterName));

		}

		if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("checkIn")) {
			System.out.println("do checkin 2");
			doCheckIn(request, response);
		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals(
						"editprofile")) {
			System.out.println("do edit profile");

			doEditProfileUpdate(request, response);
		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("resetLoc")) {
			System.out.println("do checkOut");

			doResetLocation(request, response);
		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("Redeem")) {
			System.out.println("do checkOut");

			doRedemption(request, response);
		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("login")) {
			doLogin(request, response);
		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("register")) {
			System.out.println("register");
			doRegistration(request, response);

		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("checkOut")) {
			System.out.println("checkOut");
			doCheckOut(request, response);

		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals(
						"deleteActCust")) {
			System.out.println("checkOut");
			doDeleteActCust(request, response);

		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals(
						"deleteCoupon")) {
			System.out.println("doDeleteCoupon");
			doDeleteCoupon(request, response);

		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("addCoupon")) {
			System.out.println("doAddEditCoupon");
			doAddEditCoupon(request, response);

		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType")
						.equals("searchCust")) {
			System.out.println("doSearchCust");
			doSearchCust(request, response);

		} else if (request.getParameter("adminRequestType") != null
				&& request.getParameter("adminRequestType").equals("cookie")) {
			System.out.println("doCookieLogin");

			doCookieLogin(request, response);

		} else {
			System.out.println("lol " + request.getParameter("sendUserHome"));

			if (request.getParameter("sendUserHome") == null) {
				// RequestDispatcher dispatcher =
				// ctx.getRequestDispatcher("/errorPage.jsp");

				System.out.println("send user to error error -- "
						+ request.getAttribute("errorPage"));
				// request.setAttribute("userName",request.getAttribute("sendUserHome"));

				// dispatcher.forward(request,response);
				response.sendRedirect(rootPath + "/errorPage.jsp");
				return;

			} else if (Integer.parseInt(request.getParameter("sendUserHome")) == 0) {
				// RequestDispatcher dispatcher =
				// ctx.getRequestDispatcher("/_main/userHomePage.jsp");

				System.out.println("send user to home home home -- "
						+ request.getAttribute("sendUserHome"));
				request.setAttribute("userName",
						request.getAttribute("sendUserHome"));
				ArrayList<UserData> userInfo = new ArrayList<UserData>();
				String id = request.getParameter("sendUserHome");
				userInfo = helper.getUserInfo(id);
				System.out.println("login array size  " + userInfo.size());
				if (userInfo.size() == 0) {
					System.out.println("login array size  " + userInfo.size());

				}

				// dispatcher.forward(request,response);
			}
		}

	}

}
